package com.leo;

import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import javafx.util.Callback;

/**
 * @ClassName ListViewDemo
 * @Description 鼠标放到listview 显示效果
 * @Author wangss
 * @date 2020.06.13 15:19
 * @Version 1.0
 */
public class ListViewDemo extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        AnchorPane anchorPane = new AnchorPane();
        anchorPane.setStyle("-fx-background-color: #fff2f2");

        Button sort = new Button("排序");

        ListView<String> listView = new ListView();
        listView.setPrefWidth(400d);
        listView.setPrefHeight(400d);

        ObservableList<String> obsList = listView.getItems();
        obsList.add("123");
        obsList.add("4561");
        obsList.add("4562");
        obsList.add("1234");
        obsList.add("4565");
        obsList.add("456f");
        obsList.add("456d");

        anchorPane.getChildren().addAll(sort, listView);
        AnchorPane.setTopAnchor(sort, 100d);
        AnchorPane.setLeftAnchor(sort, 100d);
        AnchorPane.setTopAnchor(listView, 140d);
        AnchorPane.setLeftAnchor(listView, 100d);

        Scene scene = new Scene(anchorPane);
        primaryStage.setScene(scene);
        primaryStage.setWidth(800d);
        primaryStage.setHeight(800d);
        primaryStage.setTitle("搜索功能");
        primaryStage.setResizable(false);
        primaryStage.show();

        listView.setCellFactory(new Callback<ListView<String>, ListCell<String>>() {

            int position = 0;

            @Override
            public ListCell<String> call(ListView<String> param) {

                Label label = new Label();
                label.setPrefHeight(20d);
                label.setFont(new Font(15));

                ListCell<String> listCell = new ListCell<String>() {
                    @Override
                    protected void updateItem(String item, boolean empty) {
                        super.updateItem(item, empty);

                        if (!empty) {
                            label.setText(item);
                            this.setGraphic(label);
                        }
                    }
                };

                listCell.hoverProperty().addListener((observable, oldValue, newValue) -> {
                    if (newValue && !"".equals(label.getText())) {
                        position = param.getItems().indexOf(label.getText());
                        label.setPrefHeight(40d);
                        label.setFont(new Font(40d));
                        param.getFocusModel().focus(position);
                    } else {
                        label.setPrefHeight(20d);
                        label.setFont(new Font(15));
                    }
                });

                return listCell;
            }
        });
    }
}
